<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Relations\UserInfoTrait;
use Illuminate\Support\Facades\DB;

/**
 *
 * @author Joker
 *
 */
class CompanyJobs extends Model
{
    use SoftDeletes,UserInfoTrait;
    protected $table='company_jobs';

    protected $primaryKey = 'jobs_id';

    protected $fillable = array('jobs_id','cat_id','company_id','user_id','title','salary','degree',
        'area','gznx','gzxz','intro','description','test','exam_id','status','recruitment','published_at','validated_at',
        'recruitment','salary_lower','salary_high'
    );

    protected $dates=['deleted_at'];

    /**
     * 获取公司信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function company()
    {
        return $this->hasOne('App\Models\Company','id','company_id');
    }

    /**
     * 获取所属地区信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function areas()
    {
        return $this->hasOne('App\Models\Area','area_id','area');
    }

    /**
     * 获取分类信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function category()
    {
        return $this->hasOne('App\Models\CompanyJobsType','cat_id','cat_id');
    }

    /**
     * 获取相关试卷
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function examination()
    {
        return $this->hasOne('App\Models\Examination','id','exam_id');   
    }
    
    /**
     * 获取职位列表数据
     */
    public function getJobsList($skip,$pagesize,$where,$sort = 0)
    {
        $query = DB::table($this->table)->select('company_jobs.*')->whereNull('deleted_at');
        if($where['areaid']>0)
        {
            $query->where('company_jobs.area','=',$where['areaid']);
        }
        if($where['comtype']>0)
        {
            $query->leftJoin('company_type_link','company_jobs.company_id','=','company_type_link.company_id')
                ->where('company_type_link.type_id','=',$where['comtype']);
        }
        if($where['develop']>0)
        {
            $query->leftJoin('company','company_jobs.company_id','=','company.id')
                ->where('company.develop_id','=',$where['develop']);
        }
        if($where['gznx'] >= 0 && $where['gznx'] <100)
        {
            $query->where('company_jobs.gznx','=',$where['gznx']);
        }
        if($where['degree']>= 0 && $where['degree'] <100)
        {
            $query->where('company_jobs.degree','=',$where['degree']);
        }
        if($where['gzxz']>= 0 && $where['gzxz'] <100)
        {
            $query->where('company_jobs.gzxz','=',$where['gzxz']);
        }
        if($where['keyword'])
        {
            $query->where('title','like','%'.$where['keyword'].'%');
        }
            $query->where('company_jobs.status','=',1);
        switch ($sort){
            case 1:
                $query->orderBy('company_jobs.updated_at','desc');break;
            default:
                $query->orderBy('company_jobs.recruitment','desc')->orderBy('company_jobs.validated_at','desc')->orderBy('company_jobs.created_at','desc');

        }
        $result = $query->orderBy('company_jobs.jobs_id','desc')->skip($skip)->take($pagesize)->get();

        return $result;

    }
}